package school.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import school.domain.News;
import school.mapper.NewsMapper;
import school.support.BaseController;
import school.support.util.ParamUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;

@Controller
@RequestMapping("news")
public class NewsController extends BaseController {

    @Autowired
    private NewsMapper newsMapper;

    /**
     * 新闻列表
     *
     * @param news
     * @param pageNum
     * @param model
     * @return
     */
    @RequestMapping("list")
    public String list(News news, @RequestParam(defaultValue = "1") Integer pageNum, Model model) {
        PageHelper.startPage(pageNum, 10);
        Example example = new Example(News.class);
        if(news.getName()!=null)
            example.createCriteria()
                    .andLike("name", "%" + news.getName() + "%");
        PageInfo<News> pageInfo = new PageInfo<>(newsMapper.selectByExample(example), 5);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("requestUrl", request.getRequestURI() + "?" + ParamUtils.params2String(request));
        return "news/list";
    }

    /**
     * 删除新闻
     *
     * @param id
     * @return
     */
    @RequestMapping("del/{id}")
    public String del(@PathVariable Integer id) {
        newsMapper.deleteByPrimaryKey(id);
        return refresh();
    }

    /**
     * 添加新闻
     *
     * @return
     */
    @GetMapping("add")
    public String add() {
        return "news/add";
    }

    /**
     * 添加日程
     *
     * @return
     */
    @PostMapping("add")
    public String add(News news, MultipartFile file) {
        String path = saveFile(file);
        news.setImg(path);
        newsMapper.insertSelective(news);
        return "redirect:list.do";
    }

    /**
     * 新闻详情
     *
     * @param id
     * @param model
     * @return
     */
    @GetMapping("detail/{id}")
    public String detail(@PathVariable Integer id, Model model) {
        News news = newsMapper.selectByPrimaryKey(id);
        model.addAttribute("news", news);
        return "news/detail";
    }

    /**
     * 更新
     *
     * @param news
     * @return
     */
    @PostMapping("update")
    public String update(News news, MultipartFile file) {
        if(file != null && !file.isEmpty()){
            String path = saveFile(file);
            news.setImg(path);
        }
        newsMapper.updateByPrimaryKeySelective(news);
        return "redirect:list.do";
    }

    @GetMapping("info/{id}")
    public String info(@PathVariable Integer id, Model model) {
        News news = newsMapper.selectByPrimaryKey(id);
        model.addAttribute("news", news);
        return "news/info";
    }

}
